package com.ibm.db2oncampus.portal.usuario.persistence.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.MatchMode;

import com.ibm.db2oncampus.portal.framework.orm.hibernate.support.HibernateDaoSupport;
import com.ibm.db2oncampus.portal.usuario.domain.Role;
import com.ibm.db2oncampus.portal.usuario.persistence.RoleDao;

public class RoleDaoImpl extends HibernateDaoSupport<Role> implements RoleDao {

	public Role findRoleByName(String roleName) {
		Criteria criteria = getSession().createCriteria(Role.class);
		criteria.add(Expression.like("name", roleName, MatchMode.EXACT));

		List<Role> allRolesFound = (List<Role>) criteria.list();

		Role foundRole = null;
		
		if(allRolesFound != null && !allRolesFound.isEmpty()) {
			foundRole = (Role) criteria.list().get(0);
		}

		return foundRole;
	}
	
	public List<Role> listAnonymousRoles() {
		Criteria criteriaRole = getSession().createCriteria(Role.class);
		criteriaRole.add(Expression.eq("anonymous", Boolean.TRUE));
		return criteriaRole.list();
	}
}